import React from 'react'
import { Cell, CellBody, CellFooter, CellHeader, Panel, Dialog } from 'react-weui'
import { browserHistory } from 'react-router'
import {connectUser} from '../../container/user/connectUser'



class MainTitle extends React.Component {
	constructor(props) {
		super(props)
        this.state = {
            show:false,
            authName:null,
            logout:false,
            style: {
                title: '确定退出',
                buttons: [{
                    type: 'primary',
                    label: '确定',
                    onClick: this.handleLogOut.bind(this)
                }, {
                    type: 'default',
                    label: '取消',
                    onClick: this.showDialog.bind(this)
                }]
            }
        }
	}
  componentDidMount() {
    // console.log(userId);
    // if(userId!=null){
    //  this.setState({
    //     display:'none',
    //     display2:'block',
    //     show:true,
    //  })
    // }
    // if(userId==null){
    //   this.setState({
    //     display:'block',
    //     display2:'none',
    //  })
    // }
  }
	handleGoBack(){
		history.go(-1);
	}
	handleLogin() {
        const storeId = this.props.storeId;
        browserHistory.push(`/store/${storeId}/user/login`)
	}
	handleSignUp() {
        const storeId = this.props.storeId;
        browserHistory.push(`/store/${storeId}/user/signup`)
	}
    handleLogOut() {
        this.props.logoutActions.logoutUser();
    }
  showDialog(){
    this.setState({
        show: !this.state.show
    });
  }
   // componentWillReceiveProps(nextProps) {
   //      if(nextProps.logout.isLogouted == true&&this.state.logout==true) {
   //          const storeId = this.props.storeId;
   //          // browserHistory.push(`/store/${storeId}`);
   //          this.setState({
   //              logout:false
   //          })
   //      }
   //  }
	render() {
        const session = localStorage.getItem('session');
        const isAuth = localStorage.getItem('isAuthenticated');
		return (
			<Panel style={{
           	    background: '#F7F7FA',
           	    position: 'fixed',
           	    top: 0,
           	    width: '100%',
           	    zIndex: 5
           	}}>
            <Cell >
                <CellHeader>
                   <i className="iconfont icon-back" style={{
                       fontSize: '1.4em',
                       marginRight: '10px',
                       color: 'gray'
                   }} onClick={this.handleGoBack.bind(this)}></i>
                </CellHeader>
                <CellBody >
                   缝客网易购
                </CellBody>
                <CellFooter>
                {
                    session&&isAuth? 
                     <CellFooter>
                        <a className="title-tag">已登录</a>
                        <span>&nbsp;|&nbsp;</span>
                        <a className="title-tag" onClick={()=>this.showDialog()}>退出</a>
                        <Dialog  type="ios" title={this.state.style.title} buttons={this.state.style.buttons} show={this.state.show}></Dialog>
                    </CellFooter>
                    :
                    <CellFooter>
                        <a onClick={this.handleLogin.bind(this)} className="title-tag">登录</a>
                        <span>&nbsp;|&nbsp;</span>
                        <a onClick={this.handleSignUp.bind(this)} className="title-tag">注册</a>
                    </CellFooter>
                }        
                </CellFooter>
            </Cell>
            </Panel>
		);
	}
}
export default connectUser(MainTitle)

